package com.wa.util;
import com.wa.graphql.enums.ObjectTypeEnum;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import org.springframework.util.ReflectionUtils;

/**
 * graphql 实体对应的schema定义
 * <p> aaron.qiu </p>
 * <p>Version: 1.0.0</p>
 * <p>Create Date： 2018-11-07 10:42:37 </p>
 * <p>Copyright (c) 2018 ~ 2019 Allchips版权所有</p>
 */
public class GraphSchemaBeanUtils {

    public static GraphQLObjectType.Builder beanSchema(Class<?> classic, GraphQLObjectType.Builder builder) {
//        GraphQLOutputType userOutputType = null;
//            builder.field(GraphQLFieldDefinition.newFieldDefinition().name("id").type(Scalars.GraphQLLong))
//                .field(GraphQLFieldDefinition.newFieldDefinition().name("userId").type(Scalars.GraphQLString))
//                .field(GraphQLFieldDefinition.newFieldDefinition().name("userName").type(Scalars.GraphQLString))
//                .field(GraphQLFieldDefinition.newFieldDefinition().name("createTime").type(new GraphQLDate()))
//                .build();
        ReflectionUtils.doWithFields(classic, field -> {
            try {
                if (null != ObjectTypeEnum.getScalarType(field.getType().getTypeName())) {
//                System.out.println(field.getName() + "---" + field.getType().getTypeName() + "----" + ObjectTypeEnum.getScalarType(field.getType().getTypeName()).getScalarType());
                    builder.field(GraphQLFieldDefinition.newFieldDefinition().name(field.getName()).type(ObjectTypeEnum.getScalarType(field.getType().getTypeName()).getScalarType()));

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
//        GraphQLOutputType userOutputType = builder.build();
        return builder;
    }

}
